<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="用户信息">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.keyword"
                        placeholder="用户编号/昵称/手机号码"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="注册时间">
                    <daterange-picker
                        v-model:startTime="queryParams.startTime"
                        v-model:endTime="queryParams.endTime"
                    />
                </el-form-item>
                <el-form-item label="注册来源">
                    <el-select class="w-[280px]" v-model="queryParams.channel">
                        <el-option
                            v-for="(item, key) in ClientMap"
                            :key="key"
                            :label="item"
                            :value="key"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="用户编号" prop="sn" min-width="120" />
                <el-table-column label="头像" min-width="100">
                    <template #default="{ row }">
                        <el-avatar :src="row.avatar" :size="50" />
                    </template>
                </el-table-column>
                <el-table-column label="昵称" prop="nickname" min-width="100" />
                <el-table-column label="账号" prop="account" min-width="100" />
                <el-table-column label="手机号码" prop="mobile" min-width="100" />
                <el-table-column label="注册来源" prop="channel" min-width="100" />
                <el-table-column label="注册时间" prop="createTime" min-width="120" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <div>
                                <el-button v-perms="['user:detail']" type="primary" link>
                                    <router-link
                                        class="primary"
                                        :to="`/consumer/detail?id=${row.id}`"
                                        >详情</router-link
                                    >
                                </el-button>
                            </div>
                            <div class="flex items-center">
                                <el-dropdown>
                                    <el-button type="primary" link>
                                        更多
                                        <el-icon class="el-icon--right">
                                            <arrow-down />
                                        </el-icon>
                                    </el-button>
                                    <template #dropdown>
                                        <el-dropdown-menu>
                                            <el-dropdown-item>
                                                <el-button
                                                    link
                                                    @click="adjustBalance(row.userMoney, row.id)"
                                                    v-perms="['user:money:update']"
                                                    >调整余额</el-button
                                                >
                                            </el-dropdown-item>
                                            <el-dropdown-item
                                                ><el-button
                                                    link
                                                    @click="adjustIntegral(row.userPoints, row.id)"
                                                    v-perms="['user:points:update']"
                                                    >调整积分</el-button
                                                ></el-dropdown-item
                                            >
                                            <el-dropdown-item
                                                ><el-button
                                                    link
                                                    @click="adjustCoupon(row.id)"
                                                    v-perms="['system:coupon:provide']"
                                                    >发放优惠券</el-button
                                                ></el-dropdown-item
                                            >
                                        </el-dropdown-menu>
                                    </template>
                                </el-dropdown>
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <layout-footer></layout-footer>
        <ls-user-coupon ref="couponRef"></ls-user-coupon>
        <ls-user-change ref="changeRef" @refresh="getLists()"></ls-user-change>
        <ls-user-integral ref="integralRef" @refresh="getLists()"></ls-user-integral>
    </div>
</template>
<script lang="ts" setup name="consumerLists">
import { usePaging } from '@/hooks/usePaging'
import { getUserList } from '@/api/consumer'
import { ClientMap } from '@/enums/appEnums'
import LayoutFooter from '@/layout/components/footer.vue'
import LsUserChange from '../components/user-change.vue'
import LsUserCoupon from '../components/user-coupon.vue'
import LsUserIntegral from '../components/user-integral.vue'
//弹框ref
const couponRef = shallowRef()
const changeRef = shallowRef()
const integralRef = shallowRef()

const queryParams = reactive({
    keyword: '',
    channel: '',
    startTime: '',
    endTime: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getUserList,
    params: queryParams
})

//调整优惠券
const adjustCoupon = (id: any) => {
    couponRef.value.open(id)
}

//调整余额
const adjustBalance = (value: any, id: any) => {
    changeRef.value.onTrigger(value, id)
}

//调整积分
const adjustIntegral = (value: any, id: any) => {
    integralRef.value.onTrigger(value, id)
}

onActivated(() => {
    getLists()
})

getLists()
</script>


// Auto-generated function
const finderAdjustBalancePanel = (value: any, id: any) => {
    changeRef.value.onTrigger(value, id)
}